package com.youmu.xxessj.ac

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.leon.lfilepickerlibrary.LFilePicker
import com.youmu.xxessj.R
import com.youmu.xxessj.ut.SAVEYSXX
import com.youmu.xxessj.ut.Utils
import kotlinx.android.synthetic.main.activity_fbsj.*
import kotlinx.android.synthetic.main.heard.*
import net.gotev.uploadservice.MultipartUploadRequest
import net.gotev.uploadservice.UploadNotificationConfig

class fbsjActivity : AppCompatActivity() {
    private lateinit var path:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fbsj)
        init_heard()
        init()
    }

    fun init_heard(){
        heard_back.setOnClickListener {
            finish()
        }
        heard_ok.text= "提交"
        heard_ok.setOnClickListener {
            initsp(path, Utils.getContext())
        }
        heard_title.text = "发布书籍"
    }
    fun init(){
        up_file.setOnClickListener {
            xzsp()
        }
    }

    private fun xzsp() {
        LFilePicker()
                .withActivity(this@fbsjActivity)
                .withRequestCode(100)
                .withFileFilter(listOf(".png",".jpg").toTypedArray())
                .withMutilyMode(false)
//                .withFileSize((500 * 1024).toLong())
                .start()
    }

    private fun initsp(path :String,  context: Context) {
        try {
            val uploadId = MultipartUploadRequest(context, SAVEYSXX)
                    // starting from 3.1+, you can also use content:// URI string instead of absolute file
                    .addFileToUpload(path, "file")
                    .addParameter("uid",intent.getStringExtra("id"))
                    .addParameter("bookname",bookname.text.toString())
                    .addParameter("bookcb",bookcb.text.toString())
                    .addParameter("bookzz",bookzz.text.toString())
                    .addParameter("jf",intent.getStringExtra("jf"))
                    .setNotificationConfig(UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload()
            finish()
        } catch (exc: Exception) {
            Log.e("AndroidUploadService", exc.message, exc)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode ==100) { // 视频
                val list = data?.getStringArrayListExtra("paths")
                path = list!![0]
                up_file.text=path
            }

        }
    }
}
